package net.oxygen.upload.util;

import lombok.Data;
import lombok.NoArgsConstructor;
import net.oxygen.upload.enums.BizCodeEnum;


import java.io.Serializable;

/**
 * <p>
 * 功能：接口统一协议工具类
 * </p>
 *
 * @author 陈伟伟
 * @since 2023/12/13 10:32
 */
@Data
@NoArgsConstructor
public class JsonData implements Serializable {

    private static final long serialVersionUID = 1L;	// 序列化版本号

    public static final int CODE_SUCCESS = 200;

    public static final int CODE_ERROR = 500;

    private int code;

    private String msg;

    private Object data;

    public JsonData(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static JsonData success() {
        return new JsonData(CODE_SUCCESS, "success", null);
    }

    public static JsonData success(Object data) {
        return new JsonData(CODE_SUCCESS, "success", data);
    }

    // 构建失败
    public static JsonData error() {
        return new JsonData(CODE_ERROR, "Internal server error", null);
    }

    public static JsonData error(String msg) {
        return new JsonData(CODE_ERROR, msg, null);
    }

    public static JsonData codeAndMsg(int code, String msg) {
        return new JsonData(code, msg, null);
    }

    public static JsonData result(BizCodeEnum codeEnum) {
        return codeAndMsg(codeEnum.getCode(), codeEnum.getMessage());
    }

    public static JsonData result(BizCodeEnum codeEnum, String msg) {
        return codeAndMsg(codeEnum.getCode(), msg);
    }

    public static JsonData resultAndData(BizCodeEnum codeEnum, Object data) {
        return new JsonData(codeEnum.getCode(), codeEnum.getMessage(), data);
    }
}
